<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 基础表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-select v-model="query.time" placeholder="年份" class="handle-select mr10">
                    <el-option key="1" label="2024" value="2024"></el-option>
                    <el-option key="2" label="2023" value="2023"></el-option>
                    <el-option key="3" label="2022" value="2022"></el-option>
                    <el-option key="4" label="2021" value="2021"></el-option>
                    <el-option key="5" label="2020" value="2020"></el-option>
                    <el-option key="6" label="2019" value="2019"></el-option>
                    <el-option key="7" label="2018" value="2018"></el-option>
                    <el-option key="8" label="2017" value="2017"></el-option>
                    <el-option key="9" label="2016" value="2016"></el-option>
                    <el-option key="10" label="2015" value="2015"></el-option>
                    <el-option key="11" label="2014" value="2014"></el-option>
                    <el-option key="12" label="2013" value="2013"></el-option>
                </el-select>

                <el-select v-model="query.meetting" placeholder="会议" class="handle-select mr10">
                    <el-option key="1" label="ACCV" value="ACCV"></el-option>
                    <el-option key="2" label="CVPR" value="CVPR"></el-option>
                    <el-option key="3" label="ECCV" value="ECCV"></el-option>
                    <el-option key="4" label="ICCV" value="ICCV"></el-option>
                    <el-option key="5" label= "WACV" value= "WACV"></el-option>
                </el-select>
                <el-input v-model="query.theme" placeholder="题目" class="handle-input mr10"></el-input>
                <el-input v-model="query.authors" placeholder="作者" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-search" @click="handleTest">搜索1</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="id" label="ID"  width="65" align="center"></el-table-column>
                <el-table-column prop="theme" label="题目"></el-table-column>
                <el-table-column prop="authors" label="作者"></el-table-column>
                <el-table-column prop="meetting" label="会议" width="80" align="center"></el-table-column>
                <el-table-column prop="time" label="年份" width="65" align="center"></el-table-column>
                <el-table-column prop="abstract" label="摘要" v-if="false"></el-table-column>
                    <el-table-column label="摘要" width="180" align="center">
                        <template #default="scope">
                            <el-button type="text" icon="el-icon-full-screen" @click="openAbstract(scope.$index, scope.row)">查看
                            </el-button>

                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="180" align="center">
                        <template #default="scope">
                            <el-button type="text" icon="el-icon-tickets" @click="handleSeePdf(scope.$index, scope.row)">预览
                            </el-button>
                            <el-button type="text" icon="el-icon-download" 
                                @click="handleTest(scope.$index, scope.row)">下载</el-button>
                        </template>
                    </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"
                    :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" v-model="editVisible" width="30%">
            <el-form label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { fetchData } from "../api/index";
import axios from 'axios';

export default {
    name: "basetable",
    setup() {
        const query = reactive({
            theme: "",
            authors: "",
            time:'2024',
            meetting:'CVPR',
            pageIndex: 1,
            pageSize: 10,
        });
        const tableData = ref([]);
        const pageTotal = ref(0);
        // 获取表格数据
        const getData = () => {
            fetchData(query).then((res) => {
                const data= res.assay_detail;
                console.log(Object.keys(data).length)
                const result = data.filter(
                    item => {
                        if (query.theme!=="")
                            // console.log("xxxxxx")
                            return item.time===query.time && item.meetting===query.meetting && query.theme===item.theme;
                        if  (query.theme!=="" &&  query.authors!=="")
                            return item.time===query.time && item.meetting===query.meetting && query.theme===item.theme && query.authors===item.authors;
                        // console.log("duiduidu")
                        return item.time===query.time && item.meetting===query.meetting;
                    }
                    

                );
                // const result = data.filter(item => item.time===query.time && item.meetting===query.meetting)
                pageTotal.value = Object.keys(result).length || 0;
                const ans1 = Array.isArray(result) ?result.slice(query.pageIndex*query.pageSize-query.pageSize, query.pageIndex*query.pageSize) : [];
                let id_Index= query.pageIndex*query.pageSize-query.pageSize+1
                console.log(id_Index)
                ans1.forEach(item => {
                    console.log(item.id,id_Index)
                    item.id = id_Index; // 修改id值，例如将id加100
                    id_Index+=1
                });
                tableData.value = ans1
            });
        };
        getData();


        // 查询操作
        const  handleSearch = () => {
            query.pageIndex=1
            query.pageSize=10
        //     setTimeout(() => {
        //     console.log('结束');
        // }, 2000); 
            getData();
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.pageIndex = val;
          
            getData();
        };

        // 删除操作
        const handleDelete = (index) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
                type: "warning",
            })
                .then(() => {
                    ElMessage.success("删除成功");
                    tableData.value.splice(index, 1);
                })
                .catch(() => {});
        };
        // 删除操作
        const params = new URLSearchParams();
        params.append("project",'default');
        params.append("spider",'search_assay');
        const handleTest = (index) => {
            axios.post('/api/schedule.json', params,{
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
            }
        
        )
            .then(response => {
                console.log('Job ID:', response);
                
            })
            .catch(error => {
                console.error('Error:', error);
            });

        };

        // 表格编辑时弹窗和保存
        const editVisible = ref(false);
        let form = reactive({
            name: "",
            address: "",
        });
        let idx = -1;
        const handleEdit = (index, row) => {
            idx = index;
            Object.keys(form).forEach((item) => {
                form[item] = row[item];
            });
            editVisible.value = true;
        };
        const handleSeePdf = (index, row) => {
            window.open('https://openaccess.thecvf.com/content/CVPR2024/papers/Zeng_Unmixing_Diffusion_for_Self-Supervised_Hyperspectral_Image_Denoising_CVPR_2024_paper.pdf', '_blank');
        };
        const openAbstract = (index, row) => {

            idx = index;
            const data =tableData.value
            const result = data.find(item => item.id === idx);
            ElMessageBox.alert(row.abstract, "摘要", {
            confirmButtonText: '确定',
            customClass: 'custom-message-box',
            callback: action => {
            ElMessage({
                type: 'info',
                message: `action: ${action}`
            });
            }
        });
        };
        const saveEdit = () => {
            editVisible.value = false;
            ElMessage.success(`修改第 ${idx + 1} 行成功`);
            Object.keys(form).forEach((item) => {
                tableData.value[idx][item] = form[item];
            });
        };

        return {
            query,
            tableData,
            pageTotal,
            editVisible,
            form,
            handleSearch,
            handlePageChange,
            handleDelete,
            handleEdit,
            saveEdit,
            openAbstract,
            handleSeePdf,
            handleTest,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.custom-message-box .el-message-box {
  width: 500px; /* 设置宽度 */
  height: 300px; /* 设置高度 */
}

.custom-message-box .el-message-box__content {
  overflow: auto; /* 如果内容超出，可以滚动 */
  max-height: 250px; /* 内容区最大高度 */
}
</style>
